package com.ysm.smfu.protocal;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.ysm.smfu.R;
import com.ysm.smfu.activity.All_LoginActivity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.Md5Utils;
import com.ysm.smfu.utils.SignUtil;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.io.File;
import java.sql.Timestamp;
import java.util.List;
import java.util
        .Random;

import okhttp3.Call;

/**
 * Created by Administrator on 2016/12/14.
 */
public class BaseProtocal {

    private static String loginId;
    private String nonce;
    private String timestamp;
    private String signLogin;
    private String pwd;
    private String name;
    private static String userId;
    private boolean isLogined;

    /**
     * 默认为post请求
     *
     * @param isLogined 是否需要loginId
     */
    private BaseProtocal(boolean isLogined) {
        this.isLogined = isLogined;
        nonce = String.valueOf(new Random().nextInt());
        timestamp = new Timestamp(System.currentTimeMillis()).toString();
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        userId = currShared.getString(SpKey.USERID, "");
        if (isLogined) {
            pwd = currShared.getString(SpKey.PWD, "");
            loginId = currShared.getString(SpKey.LOGINID, "");
            signLogin = SignUtil.signLogin(name, pwd, timestamp, nonce);
        }
    }

    private BaseProtocal() {
    }

    /**
     * 将userId，和loginId置为空
     */
    public static void setNull() {
        userId = "";
        loginId = "";
    }

    public static BaseProtocal builder(boolean isLogined) {
        return new BaseProtocal(isLogined);
    }

    /**
     * 是否跳往登录页,主要用于判断，与builder(isNeedLoginId, isNeedUserId, isNeedLogin, activity)方法相比，可以减少对象的创建
     *
     * @param isNeedGoLogin 如果没有登录是否需要去登陆
     * @param isNeedLoginId 是否需要loginId
     * @return true 跳往登录页
     */
    public static boolean isGoLogin(boolean isNeedLoginId, boolean isNeedGoLogin, Activity activity) {
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(loginId)) {
            new BaseProtocal(isNeedLoginId);
        }
        if (isNeedLoginId) {
            if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(loginId)) {
                if (isNeedGoLogin)
                    toLogin(activity);
                return true;
            }
        } else if (TextUtils.isEmpty(userId)) {
            if (isNeedGoLogin)
                toLogin(activity);
            return true;
        }
        return false;
    }

    /**
     * 需要loginId，没登陆不需要去登陆
     *
     * @param isNeedLoginId
     * @return
     */
    public static boolean isGoLogin(boolean isNeedLoginId) {
        return isGoLogin(isNeedLoginId, false, null);
    }

    /**
     * 需要loginId，没登陆需要去登陆
     *
     * @param isNeedLoginId
     * @param activity
     * @return
     */
    public static boolean isGoLogin(boolean isNeedLoginId, Activity activity) {
        return isGoLogin(isNeedLoginId, true, activity);
    }

    /**
     * 去登陆
     *
     * @param activity
     */
    private static void toLogin(Activity activity) {
        Intent intent = new Intent(activity, All_LoginActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_bottom_no);
    }

    /**
     * 返回post请求代理对象
     *
     * @param url
     * @return
     */
    private PostFormBuilder getPostFormBuilder(String url) {
        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(url)
                .addParams("nonce", nonce)
                .addParams("timestamp", timestamp);
        if (isLogined)
            postFormBuilder.addParams("loginId", loginId)
                    .addParams("signLogin", signLogin);
        return postFormBuilder;
    }

    /**
     * 返回get请求代理对象
     *
     * @param url
     * @return
     */
    private GetBuilder getGetBuilder(String url) {
        GetBuilder getBuilder = OkHttpUtils.get().url(url)
                .addParams("nonce", nonce)
                .addParams("timestamp", timestamp);
        if (isLogined)
            getBuilder.addParams("loginId", loginId)
                    .addParams("signLogin", signLogin);
        return getBuilder;
    }

    /**
     * 登录接口
     *
     * @param myStringCallBack
     * @param userName
     * @param psd
     * @param isOnline
     */
    public void login(final MyStringCallBack myStringCallBack, String userName, String psd, boolean isOnline) {
        String realPsd = psd;
        if (!isOnline)
            realPsd = Md5Utils.encode(psd);
        getPostFormBuilder(SpKey.BASEURL + "/login")
                .addParams("username", userName)
                .addParams("pwd", realPsd)
                .addParams("sign", SignUtil.signForLogin(nonce, realPsd, userName, timestamp))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取首页商家
     */
    public void getGoodsInfoBySelect(final MyStringCallBack myStringCallBack, String cityId, String curCityId, String lon, String lat, int currPage) {
        PostFormBuilder postFormBuilder = getPostFormBuilder(SpKey.BASEURL + "/index")
                .addParams("page", String.valueOf(currPage))
                .addParams("showNumber", SpKey.NUMBER);
        if (!TextUtils.isEmpty(cityId)) {
            //  用户选择了城市则添加cityId
            postFormBuilder.addParams("cityId", cityId);
        } else if (!TextUtils.isEmpty(curCityId)) {
            postFormBuilder.addParams("cityId", curCityId);
        }
        if (!TextUtils.isEmpty(curCityId) && !TextUtils.isEmpty(lon) && !TextUtils.isEmpty(lat)) {
            //定位成功则添加cityId
            postFormBuilder.addParams("curCityId", curCityId)
                    .addParams("lng", lon)
                    .addParams("lat", lat);
        }
        postFormBuilder.
                addParams("sign", SignUtil.signForGetGoodsInfo(nonce, timestamp, lon, lat, String.valueOf(currPage), SpKey.NUMBER, cityId, curCityId)).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        myStringCallBack.onFail(e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        myStringCallBack.onSuccess(response);
                    }
                });
    }

    /**
     * 获取首页产品
     */
    public void getHomeProduct(final MyStringCallBack myStringCallBack, int currPage) {
        getPostFormBuilder(SpKey.BASEURL + "/getGoodsInfo")
                .addParams("page", String.valueOf(currPage))
                .addParams("showNumber", SpKey.NUMBER)
                .addParams("sign", SignUtil.signForHomeProduct(nonce, timestamp, String.valueOf(currPage), SpKey.NUMBER))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        myStringCallBack.onFail(e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        myStringCallBack.onSuccess(response);
                    }
                });
    }

    /**
     * 获取产品跳转链接
     */
    public void getHomeProductUrl(final MyStringCallBack myStringCallBack, String id) {
        getPostFormBuilder(SpKey.BASEURL + "/toGoods")
                .addParams("goodsId", id)
                .addParams("sign", SignUtil.signForProductUrl(nonce, timestamp, id))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        myStringCallBack.onFail(e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        myStringCallBack.onSuccess(response);
                    }
                });

    }

    /**
     * 检测新版本
     */
    public void checkVersion(final MyStringCallBack myStringCallback, int page, int showNumber) {
        getGetBuilder(SpKey.HISTORYVERSIONURL)
                .addParams("sign", SignUtil.signForVersion(nonce, timestamp, String.valueOf(page), String.valueOf(showNumber), SpKey.TAG))
                .addParams("page", String.valueOf(page))
                .addParams("showNumber", String.valueOf(showNumber))
                .addParams("appType", SpKey.TAG)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallback.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallback.onSuccess(response);
            }
        });

    }

    /**
     * 历史版本
     * http://heizige.vicp.net/version/versionInfo
     * page 页数
     * showNumber 每页显示数量
     * appType  App类型（Android或iOS）
     */
    public void getHistoryVersion(final MyStringCallBack myStringCallBack, int page) {
        getGetBuilder(SpKey.HISTORYVERSIONURL)
                .addParams("sign", SignUtil.signForVersion(nonce, timestamp, String.valueOf(page), String.valueOf(SpKey.NUMBER), SpKey.TAG))
                .addParams("page", String.valueOf(page))
                .addParams("showNumber", String.valueOf(SpKey.NUMBER))
                .addParams("appType", SpKey.TAG)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }


    /**
     * 搜索联系人
     */
    public void getContactByPhone(final MyStringCallBack myStringCallBack, String userNam) {
        getPostFormBuilder(SpKey.BASEURL + "/user/getByPhone")
                .addParams("phone", userNam)
                .addParams("sign", SignUtil.signForZhSearch(userNam, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取订单详情
     *
     * @param myStringCallBack
     * @param page
     */
    public void getAccountDetail(final MyStringCallBack myStringCallBack, int page) {
        getPostFormBuilder(SpKey.ZHANGDANURL)
                .addParams("id", userId)
                .addParams("page", String.valueOf(page))
                .addParams("showNumber", SpKey.NUMBER)
                .addParams("nonce", nonce)
                .addParams("timestamp", timestamp)
                .addParams("sign", SignUtil.signForZhangDan(userId, String.valueOf(page), SpKey.NUMBER, nonce, timestamp))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取奖金明细
     *
     * @param myStringCallBack
     * @param page
     */
    public void getBonusDetail(final MyStringCallBack myStringCallBack, int page) {
        getPostFormBuilder(SpKey.BONUSURL)
                .addParams("id", userId)
                .addParams("page", String.valueOf(page))
                .addParams("showNumber", SpKey.NUMBER)
                .addParams("nonce", nonce)
                .addParams("timestamp", timestamp)
                .addParams("sign", SignUtil.signForZhangDan(userId, String.valueOf(page), SpKey.NUMBER, nonce, timestamp))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取分类信息
     *
     * @param myStringCallBack
     * @param page             第几页
     * @param lon              经度
     * @param lat              纬度
     * @param cityId           市ID
     * @param currCityId       当前地市ID
     * @param typeId           搜索条件
     */
    public void getCategoryInfo(final MyStringCallBack myStringCallBack, int page, String lon, String lat, String cityId, String currCityId, String typeId) {
        PostFormBuilder postFormBuilder = getPostFormBuilder(SpKey.BASEURL + "/search")
                .addParams("page", String.valueOf(page))
                .addParams("showNumber", SpKey.NUMBER)
                .addParams("sign", SignUtil.signForCategory(lon, lat, String.valueOf(page),
                        SpKey.NUMBER, nonce, timestamp, cityId, currCityId, typeId));
        if (!TextUtils.isEmpty(currCityId)) {
            postFormBuilder.addParams("curCityId", currCityId)
                    .addParams("lng", lon)
                    .addParams("lat", lat);
        }

        if (!TextUtils.isEmpty(cityId))
            postFormBuilder.addParams("cityId", cityId);
        else if (!TextUtils.isEmpty(currCityId))
            postFormBuilder.addParams("cityId", currCityId);

        if (!TextUtils.isEmpty(typeId))
            postFormBuilder.addParams("classId", typeId);
        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取商户详情
     *
     * @param myStringCallBack
     * @param businessId       商户Id
     */
    public void getBusinessInfo(final MyStringCallBack myStringCallBack, String businessId) {
        getPostFormBuilder(SpKey.BASEURL + "/getBusinessInfo")
                .addParams("businessId", businessId)
                .addParams("sign", SignUtil.signForGetBusinessInfo(businessId, nonce, timestamp))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 根据订单号获取支付信息
     *
     * @param myStringCallBack
     */
    public void getPayStateByDingDanCode(final MyStringCallBack myStringCallBack, String dingdanCode) {
        getPostFormBuilder(SpKey.BASEURL + "/business/getPayInfo")
                .addParams("payCode", dingdanCode)
                .addParams("sign", SignUtil.signForPayStateByPayCode(dingdanCode, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });

    }

    /**
     * 检测支付密码设置状态
     *
     * @param myStringCallBack
     * @param userId
     */
    public void checkZhiFuPsdState(final MyStringCallBack myStringCallBack, String userId) {
        getPostFormBuilder(SpKey.BASEURL + "/user/getPay")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForCheckZFPayState(userId, nonce,
                        timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }


    /**
     * 根据金额生成订单号
     *
     * @param myStringCallBack
     * @param totalPrice
     * @param bussinessId
     * @param userId
     * @param remark
     */
    public void createDingdanByMoney(final MyStringCallBack myStringCallBack, String totalPrice, String bussinessId, String userId, String remark) {
        PostFormBuilder postFormBuilder = getPostFormBuilder(SpKey.BASEURL + "/business/placePayOrderMoney")
                .addParams("totalPrice", totalPrice)
                .addParams("businessId", bussinessId)
                .addParams("userId", userId)
                .addParams("sign", SignUtil.signForCreateDingdan_Money(totalPrice, bussinessId, userId, loginId, signLogin, nonce, timestamp, remark));
        if (!TextUtils.isEmpty(remark)) {
            postFormBuilder.addParams("remark", remark);
        } else
            postFormBuilder.addParams("remark", "");

        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取验证码
     *
     * @param myStringCallBack
     * @param num
     * @param type
     * @param zhifupsd
     * @param zhifupsd2
     */
    public void getYzCode(final MyStringCallBack myStringCallBack, String num, String type, String zhifupsd, String zhifupsd2) {
        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(SpKey.BASEURL + "/user/send")
                .addParams("userName", num)
                .addParams("nonce", nonce)
                .addParams("timestamp", timestamp)
                .addParams("type", type);
        if (!TextUtils.isEmpty(zhifupsd2) || !TextUtils.isEmpty(zhifupsd))
            postFormBuilder.addParams("sign", SignUtil.signForGetYzCode(nonce, timestamp, num, type, signLogin, loginId))
                    .addParams("signLogin", signLogin)
                    .addParams("loginId", loginId);
        else
            postFormBuilder.addParams("sign", SignUtil.signForGetYzCode(nonce, timestamp, num, type, "", ""));

        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 检测验证码--修改支付密码
     *
     * @param myStringCallBack
     * @param num
     * @param code
     * @param type
     * @param zhifupsd
     * @param zhifupsd2
     */
    public void checkYzCode(final MyStringCallBack myStringCallBack, String num, String code, String type, String zhifupsd, String zhifupsd2) {
        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(SpKey.BASEURL + "/user/forCode")
                .addParams("phone", num)
                .addParams("code", code)
                .addParams("nonce", nonce)
                .addParams("timestamp", timestamp)
                .addParams("type", type);
        if (!TextUtils.isEmpty(zhifupsd2) || !TextUtils.isEmpty(zhifupsd))
            postFormBuilder.addParams("sign", SignUtil.signForCheckYzCode(nonce, timestamp, num, type, code, signLogin, loginId))
                    .addParams("signLogin", signLogin)
                    .addParams("loginId", loginId);
        else
            postFormBuilder.addParams("sign", SignUtil.signForCheckYzCode(nonce, timestamp, num, type, code, "", ""));

        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 检测验证码--修改登录密码
     */
    public void checkYzCode(final MyStringCallBack myStringCallBack, String phoneNum, String code, String loginPsd) {

        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(SpKey.BASEURL + "/user/forCode")
                .addParams("phone", phoneNum)
                .addParams("code", code)
                .addParams("type", "password")

                .addParams("timestamp", timestamp)
                .addParams("nonce", nonce);
        if (TextUtils.isEmpty(loginPsd)) {
            postFormBuilder.addParams("loginId", loginId)
                    .addParams("signLogin", signLogin)
                    .addParams("sign", SignUtil.signForChechCode(code, nonce, phoneNum, timestamp, "password", signLogin, loginId));
        } else {
            postFormBuilder
                    .addParams("sign", SignUtil.signForChechCode(code, nonce, phoneNum, timestamp, "password", "", ""));
        }

        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 找回/修改登录密码时获取验证码
     */
    public void getLoginYzCode(final MyStringCallBack myStringCallBack, String phoneNum, String loginPsd) {
        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(SpKey.BASEURL + "/user/send")
                .addParams("userName", phoneNum)
                .addParams("timestamp", timestamp)
                .addParams("type", "password")
                .addParams("nonce", nonce);
        if (TextUtils.isEmpty(loginPsd)) {
            postFormBuilder.addParams("loginId", loginId)
                    .addParams("signLogin", signLogin)
                    .addParams("sign", SignUtil.signForGetYzCode(nonce, timestamp, phoneNum, "password", signLogin, loginId));
        } else {
            postFormBuilder
                    .addParams("sign", SignUtil.signForGetYzCode(nonce, timestamp, phoneNum, "password", "", ""));
        }

        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 设置登录密码
     */
    public void setLoginPsd(final MyStringCallBack myStringCallBack, String psd, String phoneNumber, String codeString) {
        String md5Psd = Md5Utils.encode(psd);
        getPostFormBuilder(SpKey.BASEURL + "/userRegister")
                .addParams("phone", phoneNumber)
                .addParams("pwd", md5Psd)
                .addParams("sign", SignUtil.signForRegister(nonce, timestamp, phoneNumber, md5Psd, codeString, "register"))
                .addParams("code", codeString)
                .addParams("type", "register")
                .build().execute(
                new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        myStringCallBack.onFail(e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        myStringCallBack.onSuccess(response);
                    }
                }
        );
    }

    /**
     * 搜索商家
     *
     * @param myStringCallBack
     * @param page
     * @param lon
     * @param lat
     * @param cityId
     * @param currCityId
     * @param keyWord
     */
    public void search(final MyStringCallBack myStringCallBack, int page, String lon, String lat, String cityId, String currCityId, String keyWord) {
        PostFormBuilder postFormBuilder = OkHttpUtils.post().url(SpKey.BASEURL + "/search")
                .addParams("page", String.valueOf(page))
                .addParams("showNumber", SpKey.NUMBER)
                .addParams("nonce", nonce)
                .addParams("timestamp", timestamp)
                .addParams("sign", SignUtil.signForSearch(lon, lat, String.valueOf(page),
                        SpKey.NUMBER, nonce, timestamp, cityId, currCityId, keyWord));
        if (!TextUtils.isEmpty(currCityId)) {
            postFormBuilder.addParams("curCityId", currCityId)
                    .addParams("lng", lon)
                    .addParams("lat", lat);
        }
        if (!TextUtils.isEmpty(cityId))
            postFormBuilder.addParams("cityId", cityId);
        else if (!TextUtils.isEmpty(currCityId))
            postFormBuilder.addParams("cityId", currCityId);

        postFormBuilder.addParams("search", keyWord)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 检测商户状态
     */
    public void chechShangHuState(final MyStringCallBack myStringCallBack) {
        getPostFormBuilder(SpKey.BASEURL + "/business/getState")
                .addParams("userId", userId)
                .addParams("sign", SignUtil.signForChechShangState(userId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 上传商户信息
     *
     * @param myStringCallBack
     */
    public void udLoadInfo(final MyStringCallBack myStringCallBack, String userId, String select_id, String shanghuName, String num, String profile
            , String serveceNum, String provinceCode, String cityCode, String countryCode, String lng, String lat, String detailAddr, String isAbout
            , File zhizhaoF, File shenfenF, List<File> files, Activity tag) {
        PostFormBuilder postFormBuilder = getPostFormBuilder(SpKey.BASEURL + "/business/saveBusiness")
                .addParams("userId", userId)
                .addParams("classId", select_id)
                .addParams("name", shanghuName)
                .addParams("phone", num)
                .addParams("discount", profile)
                .addParams("telephone", serveceNum)
                .addParams("provinceId", provinceCode)
                .addParams("cityId", cityCode);
        if (!TextUtils.equals(countryCode, "820210") && !TextUtils.equals(countryCode, "820110"))
            postFormBuilder.addParams("countryId", countryCode);
        postFormBuilder.addParams("lng", lng)
                .addParams("lat", lat)
                .addParams("isAbout", "01")
                .addFile("businessPhotos", zhizhaoF.getName(), zhizhaoF)
                .addFile("cardPhotos", shenfenF.getName(), shenfenF)
                .tag(tag)
//                .addParams("jointPhone", "18332537976")
                .addParams("sign", SignUtil.signForShangHu(userId, select_id, shanghuName, num, profile,
                        serveceNum, provinceCode, nonce, timestamp, cityCode, countryCode,
                        lng, lat, detailAddr, isAbout, signLogin, loginId));

        for (int i = 0; i < files.size(); i++) {
            postFormBuilder.addFile("aPhotos", files.get(i).getName(), files.get(i));
        }
        if (!TextUtils.isEmpty(detailAddr))
            postFormBuilder.addParams("address", detailAddr);
        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 去余额付款
     */
    public void toPayMoney(final MyStringCallBack myStringCallBack, String businessId, String jifen, String psd, String money, String level) {
        if (TextUtils.isEmpty(money))
            money = "0";
        if (TextUtils.isEmpty(jifen))
            jifen = "0";
        PostFormBuilder postFormBuilder = getPostFormBuilder(SpKey.BASEURL + "/pay/lineBalancePay")
                .addParams("id", userId)
                .addParams("payPwd", psd)
                .addParams("businessId", businessId)
                .addParams("tag", SpKey.TAG)
                .addParams("sign", SignUtil.signForPayMonty(nonce, timestamp, signLogin, loginId, userId, psd, money, jifen, businessId, SpKey.TAG, level))
                .addParams("userLevel", level)
                .addParams("money", money)
                .addParams("points", jifen);
        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 修改支付密码
     */
    public void changeZhiFuPsd(final MyStringCallBack myStringCallBack, String userId, String psd, String xiugai, String code) {
        PostFormBuilder postFormBuilder = getPostFormBuilder(SpKey.BASEURL + "/user/updatePayPwd")
                .addParams("id", userId)
                .addParams("payPwd", psd);
        if (!TextUtils.isEmpty(xiugai)) {
            postFormBuilder.addParams("type", "password")
                    .addParams("code", code)
                    .addParams("phone", name)
                    .addParams("sign", SignUtil.signForXiuGaiZhiFuPsd(userId, psd, nonce, timestamp, signLogin, loginId, "password", code, name));
        } else
            postFormBuilder.addParams("sign", SignUtil.signForXiuGaiZhiFuPsd(userId, psd, nonce, timestamp, signLogin, loginId, "", "", ""));
        postFormBuilder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 从登录页过来的找回密码
     */
    public void modifyLoginPagePsd(final MyStringCallBack myStringCallBack, String fPsd, String sPsd, String phone, String code) {
        String md5Fpsd = Md5Utils.encode(fPsd);
        String md5psd = Md5Utils.encode(sPsd);
        // 修改登录密码
        getPostFormBuilder(SpKey.BASEURL + "/unLoginForgetPwd")
                .addParams("phone", phone)
                .addParams("code", code)
                .addParams("pwd", md5Fpsd)
                .addParams("confirmPwd", md5psd)
                .addParams("sign", SignUtil.signForModifyLoginPsd(code, md5Fpsd, md5psd, nonce, phone, timestamp, "password", "", ""))
                .addParams("type", "password")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 修改登录密码
     */
    public void modifyLoginPsd(final MyStringCallBack myStringCallBack, String fPsd, String sPsd, String phone, String code) {
        String md5Fpsd = Md5Utils.encode(fPsd);
        String md5psd = Md5Utils.encode(sPsd);
        // 修改登录密码
        getPostFormBuilder(SpKey.BASEURL + "/user/forgetPwd")
                .addParams("phone", phone)
                .addParams("code", code)
                .addParams("pwd", md5Fpsd)
                .addParams("confirmPwd", md5psd)
                .addParams("sign", SignUtil.signForModifyLoginPsd(code, md5Fpsd, md5psd, nonce, phone, timestamp, "password", signLogin, loginId))
                .addParams("type", "password")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 登录状态下，获取用户信息
     */
    public void getUserInfo(final MyStringCallBack myStringCallBack) {
        getPostFormBuilder(SpKey.BASEURL + "/user/userCenter")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForGetUserInfo(userId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 非登录状态下，获取用户信息---用户在商城登录
     */
    public void getUserInfo(final MyStringCallBack myStringCallBack, String userId) {
        getPostFormBuilder(SpKey.BASEURL + "/getUserInfo")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForGetUserInfoByUserId(userId, nonce, timestamp))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取详细订单数据
     */
    public void getMingXiData(final MyStringCallBack myStringCallBack, String businessId, int currPage) {
        getPostFormBuilder(SpKey.BASEURL + "/business/getPayList")
                .addParams("businessId", businessId)
                .addParams("sign", SignUtil.signForShouKuanMingXI(businessId, nonce, timestamp, signLogin, loginId, currPage, SpKey.NUMBER))
                .addParams("page", String.valueOf(currPage))
                .addParams("showNumber", SpKey.NUMBER)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取邀请好友url
     */
    public void getInvitUrl(final MyStringCallBack myStringCallBack, String codePre, String codeSuf) {
        getPostFormBuilder(SpKey.INVIT_URL)
                .addParams("codePre", codePre)
                .addParams("codeSuf", codeSuf)
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForInvitF(userId, nonce, timestamp, signLogin, loginId, codePre, codeSuf))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 根据用户id获取用户余额
     * user/getAccount
     */
    public void getYuEr(final MyStringCallBack myStringCallBack) {
        getPostFormBuilder(SpKey.BASEURL + "/user/getAccount")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForGetUserInfo(userId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 上传头像
     */
    public void upDateImage(final MyStringCallBack myStringCallBack, File decFile, String id) {
        getPostFormBuilder(SpKey.BASEURL + "/user/saveHeadImage")
                .addFile("file", decFile.getName(), decFile)
                .addParams("id", id)
                .addParams("sign", SignUtil.signForContact(id, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 更新个人信息
     */
    public void updateInfo(final MyStringCallBack myStringCallBack, String id, String nc, String sexIndex, String dq, String sr) {
        getPostFormBuilder(SpKey.BASEURL + "/user/userAlter")
                .addParams("id", id)
                .addParams("petName", nc)
                .addParams("sex", sexIndex)
                .addParams("address", dq)
                .addParams("birthday", sr)
                .addParams("sign", SignUtil.signForUpadateInfo(id, nc, sexIndex, dq, sr, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 通过businessId获取商户信息
     */
    public void getShanghuInfoByBusinessId(final MyStringCallBack myStringCallBack, String businessId) {
        getPostFormBuilder(SpKey.BASEURL + "/business/toPrePayPage")
                .addParams("businessId", businessId)
                .addParams("sign", SignUtil.signForShanghuInfoByBussinId(businessId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 通过订单号，获取商户信息
     * /business/toGenPayPage
     * payCode:支付订单号
     *
     * @param dingdanCode
     */
    public void getShanghuInfoByDingDanCode(final MyStringCallBack myStringCallBack, String dingdanCode) {
        getPostFormBuilder(SpKey.BASEURL + "/business/toGenPayPage")
                .addParams("payCode", dingdanCode)
                .addParams("sign", SignUtil.signForPayStateByPayCode(dingdanCode, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 预支付情况下调用支付宝
     * /lineManualBalancePay
     *
     * @param businessId
     */
    public void aliPayNoCode(final MyStringCallBack myStringCallBack, String businessId, String money, String jifen) {
        if (TextUtils.isEmpty(jifen))
            jifen = "0";
        getPostFormBuilder(SpKey.BASEURL + "/pay/lineAlipayPay")
                .addParams("id", userId)
                .addParams("businessId", businessId)
                .addParams("money", money)
                .addParams("points", jifen)
                .addParams("tag", SpKey.TAG)
                .addParams("sign", SignUtil.signForAlipayNoCode(nonce, timestamp, userId,
                        businessId, money, jifen, loginId, signLogin, SpKey.TAG))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /*
     * 预支付调用微信
     */
    public void wxPrePay(final MyStringCallBack myStringCallBack, String businessId, String money, String jifen) {
        if (TextUtils.isEmpty(jifen))
            jifen = "0";
        getPostFormBuilder(SpKey.BASEURL + "/pay/lineWeChatPay")
                .addParams("id", userId)
                .addParams("businessId", businessId)
                .addParams("money", money)
                .addParams("points", jifen)
                .addParams("tag", SpKey.TAG)
                .addParams("sign", SignUtil.signForWXpayNoCode(nonce, timestamp, userId, businessId, money, loginId, signLogin, SpKey.TAG, jifen))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 上传支付密码
     */
    public void upLoadZhiFuPsd(final MyStringCallBack myStringCallBack, String psd, String userId) {
        getPostFormBuilder(SpKey.BASEURL + "/user/updatePayPwd")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForPayPsd(userId, psd, nonce, timestamp, signLogin, loginId))
                .addParams("payPwd", psd)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取商家分类
     * /business/bClassList
     */
    public void getType(final MyStringCallBack myStringCallBack) {
        if (loginId == null)
            return;
        getPostFormBuilder(SpKey.BASEURL + "/business/bClassList")
                .addParams("sign", SignUtil.signForGetType(nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 微信扫码支付
     * heizige.vicp.net/app/pay/weChatPay
     * 参数id  payCode  userLevel  tag
     */
    public void wxPayByQr(final MyStringCallBack myStringCallBack, String payCode, String userLevel) {
        getPostFormBuilder(SpKey.BASEURL + "/pay/weChatPay")
                .addParams("id", userId)
                .addParams("payCode", payCode)
                .addParams("userLevel", userLevel)
                .addParams("tag", SpKey.TAG)
                .addParams("sign", SignUtil.signForWxPay(userId, payCode, userLevel, SpKey.TAG, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 商城微信扫码支付
     * heizige.vicp.net/app/pay/weChatPay
     * 参数id  payCode  userLevel  tag
     */
    public void wxPayByQrOnline(final MyStringCallBack myStringCallBack, String payCode, String userLevel) {
        getPostFormBuilder(SpKey.BASEURL + "/pay/weChatMallPay")
                .addParams("id", userId)
                .addParams("payCode", payCode)
                .addParams("userLevel", userLevel)
                .addParams("tag", SpKey.TAG)
                .addParams("sign", SignUtil.signForWxPay(userId, payCode, userLevel, SpKey.TAG, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取系统消息
     */
    public void getRobotMsg(final MyStringCallBack myStringCallBack) {
        getPostFormBuilder(SpKey.BASEURL + "/user/getSystemMessage")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForContact(userId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 设置系统消息
     * /user/setMessage
     * id
     */
    public void setSysMsg(final MyStringCallBack myStringCallBack) {
        getPostFormBuilder(SpKey.BASEURL + "/user/setMessage")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForSetSys(userId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 获取好友申请数据
     */
    public void getSQFriends(final MyStringCallBack myStringCallBack) {
        getPostFormBuilder(SpKey.BASEURL + "/user/getFirendApply")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForContact(userId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 从网络获取联系人数据
     */
    public void getContactListFromOnLine(final MyStringCallBack myStringCallBack) {
        getPostFormBuilder(SpKey.BASEURL + "/user/friendsList")
                .addParams("id", userId)
                .addParams("sign", SignUtil.signForContact(userId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 删除好友
     * /user/deleteFirend
     * id: 用户id   firendId :好友id
     */
    public void delFriend(final MyStringCallBack myStringCallBack, String friendId) {
        getPostFormBuilder(SpKey.BASEURL + "/user/deleteFirend")
                .addParams("id", userId)
                .addParams("firendId", friendId)
                .addParams("sign", SignUtil.signForDel(userId, friendId, nonce, timestamp, signLogin, loginId))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 添加或拒绝好友申请
     * id: 用户id
     * firendId :好友id   state  1 同意，0  忽略
     */
    public void agreeOrDefauseF(final MyStringCallBack myStringCallBack, int state, String friendId) {
        getPostFormBuilder(SpKey.BASEURL + "/user/saveFirendApply")
                .addParams("id", userId)
                .addParams("firendId", friendId)
                .addParams("state", String.valueOf(state))
                .addParams("sign", SignUtil.signForAddFriend(userId, friendId, String.valueOf(state), signLogin, loginId, nonce, timestamp))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 广告请求
     */
    private static RequestCall call;

    /**
     * 获取广告页数据
     *
     * @param myStringCallBack
     */
    public void getAdData(final MyStringCallBack myStringCallBack, Activity activity) {
        call = getPostFormBuilder(SpKey.ADURL)
                .addParams("sign", SignUtil.signForAd(nonce, timestamp))
                .build();
        call.execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 取消广告数据请求
     */
    public static void cancleAdData() {
        if (call != null)
            call.cancel();
        call = null;
    }

    private static RequestCall adListCall;

    /**
     * 获取活动信息
     */
    public void getAdListData(final MyStringCallBack myStringCallBack) {
        adListCall = getPostFormBuilder(SpKey.ADLISTDATAURL)
                .addParams("sign", SignUtil.sign(nonce, timestamp))
                .build();
        adListCall.execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 是否强推
     */
    public void getUpdataConfig(final MyStringCallBack myStringCallBack, String code) {
        getPostFormBuilder(SpKey.GETUPDATECONFIG)
                .addParams("code", code)
                .addParams("tag", "Android")
                .addParams("sign", SignUtil.signForUpdataConfig(nonce, timestamp, code, "Android"))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                myStringCallBack.onFail(e);
            }

            @Override
            public void onResponse(String response, int id) {
                myStringCallBack.onSuccess(response);
            }
        });
    }

    /**
     * 取消获取活动信息
     */
    public static void cancleAdListData() {
        if (adListCall != null)
            adListCall.cancel();
        adListCall = null;
    }


}